﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.PlatForm.Dtos.Language;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 语言翻译
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.09.05
    /// </summary>
    [Route("api/PlatForm/[controller]")]
    public class LanguageController : BaseApiController
    {
        private LanguageMapBll languageMapBll = new LanguageMapBll();
        private DictionaryDataBll dictionaryDataBll = new DictionaryDataBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="typeId">分类主键</param>
        /// <returns></returns>
        [HttpGet("List/{typeId}")]
        public async Task<ResponseResult> GetList([FromQuery] PageInput input, string typeId)
        {
            var data = await languageMapBll.GetList(input, typeId);
            var output = new PageResult<LanguageListOutput>
            {
                list = await PageResultToOutput(data.list),
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="encode">主键值</param>
        /// <returns></returns>
        [HttpGet("{encode}")]
        public async Task<ResponseResult> GetInfo(string encode)
        {
            var data = await languageMapBll.GetList(encode);
            var output = new LanguageInfoOutput();
            output.translateList = data.Select(d => new TranslateListItem
            {
                fullName = d.FullName,
                language = d.Language
            }
            ).ToList();
            output.languageTypeId = data[0].LanguageTypeId;
            output.signKey = data[0].SignKey;
            return Success(output);
        }

        /// <summary>
        /// 重复验证（翻译标记）
        /// </summary>
        /// <param name="signKey">翻译标记</param>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("IsExistBySignKey")]
        public async Task<ResponseResult> IsExistBySignKey(string signKey, string id)
        {
            var data = await languageMapBll.IsExistBySignKey(id, signKey);
            return Success(data);
        }
        #endregion

        #region POST
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="encode">主键值</param>
        /// <returns></returns>
        [HttpDelete("{encode}")]
        public async Task<ResponseResult> Delete(string encode)
        {
            await languageMapBll.Delete(encode);
            return Success("删除成功");
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] LanguageCrInput input)
        {
            if (await languageMapBll.IsExistBySignKey(null, input.signKey))
            {
                return Error("标记已存在");
            }
            var languageMapList = input.translateList.MapToList<LanguageMapEntity>();
            languageMapList.ForEach(lm => lm.SignKey = input.signKey); ;
            languageMapList.ForEach(lm => lm.LanguageTypeId = input.languageTypeId);
            await languageMapBll.Create(languageMapList);
            return Success("创建成功");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="encode">主键值</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPut("{encode}")]
        public async Task<ResponseResult> Update(string encode, [FromBody] LanguageUpInput input)
        {
            if (await languageMapBll.IsExistBySignKey(encode, input.signKey))
            {
                return Error("标记已存在");
            }
            var languageMapList = input.translateList.MapToList<LanguageMapEntity>();
            languageMapList.ForEach(lm => lm.SignKey = input.signKey); ;
            languageMapList.ForEach(lm => lm.LanguageTypeId = input.languageTypeId);
            await languageMapBll.Update(encode, languageMapList);
            return Success("更新成功");
        }
        #endregion

        #region Method
        private async Task<List<LanguageListOutput>> PageResultToOutput(object obj)
        {
            DataTable dt = JsonHelper.ToTable(obj.ToJson());
            List<string> language = (await dictionaryDataBll.GetList("dc6b2542d94b407cac61ec1d59592901")).Select(m => m.EnCode).ToList();
            var data = new List<LanguageListOutput>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var output = new LanguageListOutput();
                output.languageEnCodes = new Dictionary<string, object>();
                output.encode = dt.Rows[i]["f_encode"].ToString();
                output.signKey = dt.Rows[i]["f_signkey"].ToString();
                language.ForEach(l =>
                {
                    output.languageEnCodes.Add(l.ToLower(), dt.Rows[i][l.ToLower()].ToString());
                });
                data.Add(output);
            }
            return data;
        }
        #endregion
    }
}